package com.zbl.zblproject.core.controller;

import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zbl.zblproject.core.entity.*;
import com.zbl.zblproject.core.mapper.AdminUserMapper;
import com.zbl.zblproject.core.mapper.OrderItemMapper;
import com.zbl.zblproject.core.mapper.SupplierMapper;
import com.zbl.zblproject.core.service.OrderItemService;
import com.zbl.zblproject.core.utils.ResultUtil;
import com.zbl.zblproject.erp.entity.ErpTempOrderItem;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigInteger;
import java.util.List;

/**
 * @author youlanqiang
 * @version 1.0
 * @date 2018/10/30
 */
@RestController
@RequestMapping("/order-item")
@Api(tags = "商品订单项接口")
public class OrderItemController {

    @Autowired
    private OrderItemService orderItemService;

    @Autowired
    private OrderItemMapper orderItemMapper;

    @Autowired
    private SupplierMapper supplierMapper;

    @Autowired
    private AdminUserMapper adminUserMapper;

    @DeleteMapping("/delete/{id}")
    @ApiOperation("删除商品订单项")
    public Result deleteItem(@ApiParam("订单项ID") @PathVariable int id){
        try{
            orderItemService.deleteItemById(id);
            return ResultUtil.success("成功!");
        }catch(RuntimeException e){
            return ResultUtil.error("删除失败!");
        }

    }

    @PostMapping("/update/{id}")
    @ApiOperation("更新商品订单项")
    public Result updateItemNumber(@ApiParam("订单项ID") @PathVariable int id,
                                   @ApiParam("修改数量") @RequestParam("num") int number){
            try{

            }catch(RuntimeException e){

            }
            return ResultUtil.error("该接口已经弃用,请使用更新所有订单项目接口.");
    }


    @PostMapping("/update")
    @ApiOperation("更新商品的所有订单项目")
    public Result updateItemNumbers(@ApiParam("订单项") @RequestParam("items") String items){
            try{
                List<ErpTempOrderItem> list = JSONArray.parseArray(items, ErpTempOrderItem.class);
                orderItemService.uploadNumberOfItems(list);
                return ResultUtil.success("更新成功!");
            }catch(NullPointerException e){
                return ResultUtil.error("更新失败!");
            }
    }


    @PostMapping("/listOrderItem")
    @ApiOperation("查询所有未入库订单信息")
    @ResponseBody
    public Result selectAllOrderItem(@RequestParam("orderSn")String orderSn,@RequestParam("supId")String supId){
        List<Order> orders = orderItemMapper.selectAllOrderItem(orderSn.trim(),supId.trim());
        for (Order order:orders){
            order.setCreateTime(order.getCreateTime()*1000);
            Supplier supplier = supplierMapper.selectById(order.getUserId());//这里用了userId来存供应商id
            order.setSupplierName(supplier.getSuppliersName());
            AdminUser adminUser = adminUserMapper.selectById(order.getOperatorId());
            if (adminUser==null){
                adminUser = new AdminUser();
                adminUser.setUserName("无");
            }
            order.setUser(adminUser);
        }
        return ResultUtil.success("查询所有未入库订单信息成功",orders);
    }


    @PostMapping("/deleteOrderItem/{id}")
    @ApiOperation("根据id删除未入库订单信息")
    @ResponseBody
    public Result deleteOrderItem(@ApiParam("未入库订单ID")@PathVariable String id){
        return orderItemService.deleteOrderItem(id);
    }

    @PostMapping("/updateOrderItem")
    @ApiOperation("根据id修改未入库订单数量")
    @ResponseBody
    public Result updateOrderItem(OrderItem orderItem){
        return orderItemService.updateOrderItem(orderItem);
    }

    @GetMapping(value="/exportOrderItem/{id}",produces = {"application/vnd.ms-excel;charset=UTF-8"})
    @ApiOperation("导出未入库订单信息")
    public String exportOrderItem(HttpServletResponse response, @PathVariable("id") String id) throws IOException {
        return orderItemService.exportOrderItem(response,id);
    }
//
//    @GetMapping(value="/exportOrderItems",produces = {"application/vnd.ms-excel;charset=UTF-8"})
//    @ApiOperation("导出未入库订单信息")
//    public String exportOrderItems(HttpServletResponse response) throws IOException {
//        return orderItemService.exportOrderItems(response);
//    }
}
